﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace DBUtil.UsingBlocks
{
    /// <summary>
    /// 控制阅读器自动关闭的块
    /// </summary>
    /// <remarks>
    /// 为防止占用长连接,使用后立刻释放,如下：
    /// <code>using readerBlock=db.SelectDataReader("select * from test")</code>
    /// 或
    /// <code>using(readerBlock=db.SelectDataReader("select * from test")){ }</code>
    /// 或
    /// <code>var readerBlock=db.SelectDataReader("select * from test");readerBlock.Dispose();</code>
    /// </remarks>
    public class ReaderLongConnectBlock : LongConnectBlock
    {
        /// <summary>
        /// 阅读器
        /// </summary>
        public IDataReader Reader { get; internal set; }

        /// <summary>
        /// 构建块
        /// </summary>
        /// <param name="db"></param>
        internal ReaderLongConnectBlock(DBAccess db) : base(db) { }

        /// <summary>
        /// 尝试关闭一次连接
        /// </summary>
        public override void Dispose()
        {
            try
            {
                if (!Reader.IsClosed) Reader.Close();
                base.Dispose();
            }
            catch { }
        }
    }
}
